package com.handinfo.uimenu.window;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.AbstractGameActor;
import com.handinfo.game.ChatItem;
import com.handinfo.uimenu.AbsHyperLinker;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIChat extends UIAbstractWindow
{

	private final int STATE_QUAN = 1;
	private final int STATE_WORLD = 2;
	private final int STATE_REALM = 3;
	private final int STATE_FAMILY = 4;
	private final int STATE_TEAM = 5;
	private final int STATE_XI = 6;
	private final int STATE_SI = 7;
	//
	private final String STR_POP_SILIAO = "传音入密";
	private final String STR_POP_JIAOYI = "交易";
	private final String STR_POP_CHAKAN = "查看";
	private final String STR_POP_HAOYOU = "加为好友";
	private final String STR_POP_ZUDUI = "组队";
	private final String STR_POP_SHEJIAO = "社交";
	private final String STR_POP_HEIMINGDAN = "黑名单";
	private final String STR_POP_PINGBI = "屏蔽此频道";
	private final String STR_POP_CHAKANWUPIN = "查看物品";
	private final String STR_POP_MAIL = "发送邮件";
	private final String STR_POP_BACK = "返回";
	//
	private final String STR_DESCRIBE = "点击发言";
	//
	private final String TITLE_CHAT = "聊天频道";
	private final int H_PRE = UITools.H_PRE;
	private final int NUM_LABEL_DIS = 7;// 显示的标签数目
	//
	private Image[] IMG_LABELS;
	private ChatItem[] m_items;
	private ChatItem[] m_items_curr;// 当前项
	//
	private int STATE_FIRST = STATE_QUAN;// 第一个标签的索引
	//
	private int m_y_top;
	private int m_plan;
	//
	private int H_CONTENT;
	private int H_RECT;
	private int X_LABEL_FIRST;
	private int X_ARR_LEFT;
	private int X_ARR_RIGHT;
	private int Y_LABEL;// 标签位置
	private int Y_RECT;// 内容框位置
	private int IW_LABEL = 1;
	private int W_LABEL = (UITools.W_BG - (NUM_LABEL_DIS - 1) * IW_LABEL) / NUM_LABEL_DIS;

	public UIChat(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		initData();
		initDis();
		changeState(STATE_QUAN);
		m_index = 0;
	}
	
	
	int[][] rectout;
	protected void registerTouch() {
		super.registerTouch();
		registerNormalSoft();
		int tempY = m_y + Y_LABEL;
		int tempX = m_x + X_LABEL_FIRST;
		rectout = new int[NUM_LABEL_DIS+1+2][];
		for (int i = 0; i < rectout.length; i++) {
			if (i == 0) {
				rectout[i] = new int[]{207,276,178,18};
			} else if (i == rectout.length -1) {//下
				rectout[i] = new int[]{446,241,20,32};
			} else if (i == rectout.length -2) {//上
				rectout[i] = new int[]{446,59,20,32};
			} else {
				rectout[i] = new int[]{tempX+(IW_LABEL+W_LABEL)*(i-1), tempY, W_LABEL, UITools.H_LABEL};
			}
		}
		initTouchRect(rectout);
	}



	protected void onTouchRect(int[][] rect, int touchid) {
		super.onTouchRect(rect, touchid);
		if (rectout == rect) {
			if (touchid < NUM_LABEL_DIS + 1) {
				GameTouchManager.keyPressedNum(touchid);
			} else if (touchid == NUM_LABEL_DIS + 1){//上
				GameTouchManager.keyPressedDir(1);
			} else if (touchid == NUM_LABEL_DIS + 2){//下
				GameTouchManager.keyPressedDir(3);
			}
		}
	}



	private void initData()
	{
		Vector vector = GameManager.getInstance().m_role.m_list_chats;
		if (vector == null) {
			m_items = null;
			return;
		}
		int size = vector.size();
		if (size == 0) {
			m_items = null;
		}
		else {
			m_items = new ChatItem[size];
			for (int i = 0; i < size; i++) {
				m_items[i] = (ChatItem) vector.elementAt(i);
			}
		}
	}
    
	private void initDis()
	{
		H_RECT = UITools.H_RECT - (UITools.H_LABEL + UITools.IH_RECTS);
		int NUM = (H_RECT - (UITools.IW_RECT_CONTENT << 1)) / H_PRE;
		H_CONTENT = NUM * H_PRE;
		//
//		IMG_LABELS = new Image[8];
//		for (int i = 0; i < IMG_LABELS.length; i++) {
//			IMG_LABELS[i] = Image.createImage(UIManager.IMAGE_LABEL_CHAT, i * 13, 0, 13, 13, Tools.TRANS_NONE);
//		}

		IMG_LABELS = new Image[NUM_LABEL_DIS];
		int w = UIManager.IMAGE_LABEL_CHAT.getWidth() / NUM_LABEL_DIS;
		int h = UIManager.IMAGE_LABEL_CHAT.getHeight();
		for (int i = 0; i < IMG_LABELS.length; i++) {
			IMG_LABELS[i] = Image.createImage(UIManager.IMAGE_LABEL_CHAT, i * w, 0, w, h, Tools.TRANS_NONE);
		}

		//
		X_LABEL_FIRST = ((UITools.W_BG - (NUM_LABEL_DIS * W_LABEL + (NUM_LABEL_DIS - 1) * IW_LABEL)) >> 1);// 居中显示
		X_ARR_LEFT = X_LABEL_FIRST - (UIManager.IMAGE_LABEL_ARR.getWidth()) - 2;
		X_ARR_RIGHT = UITools.W_BG - X_ARR_LEFT - (UIManager.IMAGE_LABEL_ARR.getWidth());
		Y_LABEL = UITools.INITY_CONTENT;
		Y_RECT = Y_LABEL + UITools.H_LABEL + UITools.IH_RECTS;
	}

	/**
	 * 2012-3-15 下午06:29:30 venizeng 当收到数据时，动态更新界面数据
	 */
	public static void updateChat()
	{
		UIChat chat = (UIChat) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_CHAT);
		if (chat == null) {
			return;
		}
		chat.initData();
		chat.initCurrentData();
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_items_curr == null) {
				return;
			}
			if (m_index == 0) {
				changeIndex(0);
			}
			else {
				changeIndex(m_index - 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_items_curr == null) {
				return;
			}
			if (m_index >= m_items_curr.length - 1) {// 最后一行
				changeIndex(m_items_curr.length - 1);
			}
			else {
				changeIndex(m_index + 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {//"*"
//			if (m_state == STATE_QUAN) {
//				changeState(STATE_WORLD);
//			}
//			else {
//				changeState(m_state - 1);
//			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {//"#"
//			if (m_state == STATE_WORLD) {
//				changeState(STATE_QUAN);
//			}
//			else {
//				changeState(m_state + 1);
//			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			clickItem();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY0)) {
			int num = 0 ;
			switch (m_state) {
				case STATE_QUAN:
				case STATE_WORLD:
				case STATE_XI:
				case STATE_SI:
					UIChatInsert.gotoGongliao(0);
					break;
				case STATE_REALM:
					if (GameManager.getInstance().m_role.m_faction != AbstractGameActor.FACTION_ANY) {//有阵营
						UIChatInsert.gotoGongliao(1);
					}else{
						UIChatInsert.gotoGongliao(0);
					}
					break;
				case STATE_FAMILY:
					if (GameManager.getInstance().m_role.m_position != 0) {//组队
						if (GameManager.getInstance().m_role.m_faction != AbstractGameActor.FACTION_ANY) {//有阵营
							num++ ;
						}
						UIChatInsert.gotoGongliao(num + 1) ;
					}else{
						UIChatInsert.gotoGongliao(0);
					}
					break;
				case STATE_TEAM:
					if (GameManager.getInstance().m_role.m_teamID != 0) {
						if (GameManager.getInstance().m_role.m_faction != AbstractGameActor.FACTION_ANY) {//有阵营
							num++ ;
						}
						if (GameManager.getInstance().m_role.m_position != 0) {//组队
							num++ ;
						}
						UIChatInsert.gotoGongliao(num + 1) ;
					}else{
						UIChatInsert.gotoGongliao(0);
					}
					break;
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY1)) {
			changeState(STATE_QUAN);//全
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY2)) {
			changeState(STATE_WORLD);//世
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY3)) {
			changeState(STATE_REALM);//阵营
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY4)) {
			changeState(STATE_FAMILY);//家族
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY5)) {
			changeState(STATE_TEAM);//队伍
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY6)) {
			changeState(STATE_XI);//系统
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY7)) {
			changeState(STATE_SI);//私聊
		}

	}

	/**
	 * 点击邮件选项
	 */
	private void clickItem()
	{
		if (m_items_curr == null || m_items_curr[m_index] == null) {
			return;
		}
		final String[] popmenus = getPopStrs();
		if (popmenus == null) {
			return;
		}
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		popmenu.setPopmenus(popmenus);
		popmenu.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					int index = popmenu.getCurrentIndex();
					if (popmenus[index] == STR_POP_CHAKAN) {// 查看
						actionChakan(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_CHAKANWUPIN) {// 查看物品
						actionChakanWupin(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_HAOYOU) {// 加为好友
						actionFriend(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_HEIMINGDAN) {// 黑名单
						actionHeimingdan(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_JIAOYI) {// 交易
						actionJiaoyi(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_PINGBI) {// 屏蔽此频道
						actionPinbi(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_SHEJIAO) {// 社交
						actionShejiao(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_SILIAO) {// 私聊
						actionSiliao(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_ZUDUI) {// 组队
						actionZudui(m_items_curr[m_index]);
					}
					else if (popmenus[index] == STR_POP_MAIL) {// 发送邮件
						actionMail(m_items_curr[m_index]);
					}
					UIManager.getInstance().removeWindow(popmenu);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}

			private void actionMail(ChatItem item)
			{
				if (isTalkBySelf(item)) {
					UIMail.sendMailToPlayer(item.m_name_recver);
				}
				else {
					UIMail.sendMailToPlayer(item.m_name_sender);
				}
			}

			private void actionChakan(ChatItem item)
			{
				GameManager.getInstance().m_game_message.sendAttInfos(item.m_id_sender);
			}

			private void actionChakanWupin(final ChatItem item)
			{
				AbsHyperLinker hyperLinker = item.m_hypLinker;
				if (hyperLinker == null) {
					return;
				}
				if (hyperLinker.getHyperLinkSize() == 0) {
					return;
				}
				char[] types = hyperLinker.getHyperLinkTypes();
				int num = 0;// 超链接数目
				for (int i = 0; i < types.length; i++) {
					if (types[i] == AbsHyperLinker.URL_ACTORITEM) {
						num++;
					}
				}
				if (num == 0) {
					return;
				}
				final String[] NAMES = hyperLinker.getHyperLinkContents();
				final String[] IDS = hyperLinker.getHyperLinkIds();// 超链接IDs
				final String[] items = new String[num];// 道具名称
				final String[] ids = new String[num];// 道具ID
				int index = 0;
				for (int i = 0; i < num; i++) {
					if (types[i] == AbsHyperLinker.URL_ACTORITEM) {// 只有道具
						items[index] = NAMES[i];
						ids[index] = IDS[i];
						index++;
					}
				}
				if (index == 1) {//只有一个物品
//					GameManager.getInstance().m_game_message.sendActorItemDetail(Long.parseLong(ids[0]));
					GameManager.getInstance().m_game_message.sendOtherRoleActorItemDetail(item.m_id_sender, Long
							.parseLong(ids[0]));
				}
				else {
					final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(
							UIWindowFactory.WINDOW_POPMENU);
					popmenu.setPopmenus(items);
					popmenu.addListener(new UIListener() {

						public void actionPerformed()
						{
							if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
									|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
								GameManager.getInstance().m_game_message.sendOtherRoleActorItemDetail(item.m_id_sender,
										Long.parseLong(ids[popmenu.getCurrentIndex()]));
								UIManager.getInstance().removeWindow(popmenu);
							}
							else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
								UIManager.getInstance().removeWindow(popmenu);
							}
						}
					});
				}
			}

			private void actionFriend(ChatItem item)
			{
				if (isTalkBySelf(item)) {
					GameManager.getInstance().m_game_message.sendSocialAddForName(GameManager.SOCIAL_TYPE_FRIEND,
							item.m_name_recver);
				}
				else {
					GameManager.getInstance().m_game_message.sendSocialAddForName(GameManager.SOCIAL_TYPE_FRIEND,
							item.m_name_sender);
				}

			}

			private void actionHeimingdan(ChatItem item)
			{
				if (isTalkBySelf(item)) {
					GameManager.getInstance().m_game_message.sendSocialAddForName(GameManager.SOCIAL_TYPE_BLACKLIST,
							item.m_name_recver);
				}
				else {
					GameManager.getInstance().m_game_message.sendSocialAddForName(GameManager.SOCIAL_TYPE_BLACKLIST,
							item.m_name_sender);
				}

			}

			private void actionJiaoyi(ChatItem item)
			{
				GameManager.getInstance().m_game_message.sendDealRequest(item.m_id_sender);
			}

			private void actionPinbi(ChatItem item)
			{
				GameManager.getInstance().m_game_message.sendChatPingBi(item.m_type);//屏蔽频道
			}

			private void actionShejiao(ChatItem item)
			{
				UISocialWindow.openSocialList();
			}

			private void actionSiliao(ChatItem item)
			{
				if (isTalkBySelf(item)) {
					UIChatInsert.gotoSiliao(item.m_id_sender, item.m_name_recver);
				}
				else {
					UIChatInsert.gotoSiliao(item.m_id_sender, item.m_name_sender);
				}
			}

			private void actionZudui(ChatItem item)
			{
				if (item.m_id_sender == GameManager.getInstance().m_role.m_gameID) {
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "无法和自己组队");
				}
				else {
					GameManager.getInstance().m_game_message.sendTeamRequest(item.m_id_sender);
				}
			}

			private boolean isTalkBySelf(ChatItem item)
			{
				return item.m_name_sender.equals("您");
			}
		});
	}

	private String[] getPopStrs()
	{
		ChatItem item = m_items_curr[m_index];
//		boolean is =  item.m_id_sender == ChatItem.ID_NONE || item.m_id_sender == ChatItem.ID_SYSTEM
//		|| item.m_id_sender == GameManager.getInstance().m_role.m_gameID;

		if (item.m_id_sender == GameManager.getInstance().m_role.m_gameID) {
			return null;
		}
		String[] temps = null;
		switch (item.m_type) {
			case ChatItem.TYPE_XI:
				if (isChatItemGotGood(item)) {
					temps = new String[] { STR_POP_CHAKANWUPIN, STR_POP_BACK };
				}
				else {
					return null;
				}
				break;
			case ChatItem.TYPE_QU:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
						STR_POP_ZUDUI, STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
			case ChatItem.TYPE_SI:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
						STR_POP_ZUDUI, STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
			case ChatItem.TYPE_TEAM:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
							STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
			case ChatItem.TYPE_WORLD:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
							STR_POP_ZUDUI, STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
			case ChatItem.TYPE_FAMILY:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
							STR_POP_ZUDUI, STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
			case ChatItem.TYPE_REALM:
				temps = new String[] { STR_POP_SILIAO, STR_POP_CHAKAN, STR_POP_CHAKANWUPIN, STR_POP_MAIL,
							STR_POP_ZUDUI, STR_POP_HAOYOU, STR_POP_HEIMINGDAN, STR_POP_BACK };
				break;
		}
		if (!isChatItemGotGood(item)) {
			temps = filterActorItem(temps);
		}
		return temps;
	}

	// 是否包含道具
	private boolean isChatItemGotGood(ChatItem item)
	{
		if (item.m_hypLinker == null) {
			return false;
		}
		char[] types = item.m_hypLinker.getHyperLinkTypes();
		for (int i = 0; i < types.length; i++) {
			if (types[i] == AbsHyperLinker.URL_ACTORITEM) {
				return true;
			}
		}
		return false;
	}

	// 如果不包括道具，则将“查看道具”移除
	private String[] filterActorItem(String[] items)
	{
		if (items == null) {
			return null;
		}
		if (items.length == 1 && items[0].equals(STR_POP_CHAKANWUPIN)) {
			return null;
		}
		String[] result = new String[items.length - 1];
		int index = 0;
		for (int i = 0; i < items.length; i++) {
			if (items[i].equals(STR_POP_CHAKANWUPIN)) {
				continue;
			}
			result[index] = items[i];
			index++;
		}
		return result;
	}

	private void changeIndex(int index)
	{
		// 计算下拉条百分比
		m_plan = countCurrentPlan();
		// 索引值
		if (m_items_curr == null) {
			return;
		}
		if (index < 0) {
			return;
		}
		if (index >= m_items_curr.length) {
			return;
		}
		m_index = index;
		// 相对位置
		if (m_index == 0) {// 数据最顶
			m_y_top = 0;
			return;
		}
		while (getTopY(m_index) < 0) {// 如果当前选项的顶部位置超出屏幕顶部
			m_y_top++;
		}
		while (getBottomY(m_index) > H_CONTENT) {// 如果当前选项的顶部位置超出底部
			m_y_top--;
		}
	}

	private int getTopY(int index)
	{
		return getHeight(index) + m_y_top;
	}

	private int getBottomY(int index)
	{
		return getTopY(index) + m_items_curr[m_index].getH();
	}

	private int getHeight(int index)
	{
		if (m_items_curr == null) {
			return 0;
		}
		if (index >= m_items_curr.length) {// 最大位置
			index = m_items_curr.length;
		}
		int sumH = 0;
		for (int i = 0; i < index; i++) {
			if (m_items_curr[i] == null) {
				continue;
			}
			sumH += m_items_curr[i].getH();
		}
		return sumH;
	}

	private void changeState(int state)
	{
		if (m_state == state) {
			return;
		}
		m_state = state;
		initCurrentData();
		//
		changeIndex(0);
		//
	}

	private void initCurrentData()
	{

		if (m_state < STATE_FIRST) {
			STATE_FIRST = m_state;
		}
		else {
			if (m_state - STATE_FIRST >= NUM_LABEL_DIS) {
				STATE_FIRST = m_state - NUM_LABEL_DIS + 1;
			}
		}
		//
		m_items_curr = filterChatItem2s(m_state);
	}

	// 计算当前的下拉百分比
	private int countCurrentPlan()
	{
		if (m_items_curr == null) {
			return 0;
		}
		int plan = 0;
		if (m_index == 0) {
			plan = 0;

		}
		else if (m_index == m_items_curr.length - 1) {
			plan = 100;
		}
		else {
			plan = (int) (((100 * (m_index + 1))) / m_items_curr.length);
		}
		return plan;
	}

	// 过滤当前聊天记录，和当前标签一直
	private ChatItem[] filterChatItem2s(int state)
	{
		ChatItem[] item = sieveChatItem(m_items, state);
		if (item == null) {
			return null;
		}
		ArrayList list = new ArrayList();
		if (state == STATE_QUAN) {
			for (int i = item.length - 1; i >= 0; i--) {
				list.addElement(item[i]);
			}
		}
		else {
			for (int i = item.length - 1; i >= 0; i--) {
				if (item[i].m_type == getChatType(state)) {
					list.addElement(item[i]);
				}
			}
		}
		int size = list.size();
		if (size == 0) {
			return null;
		}
		ChatItem[] items = new ChatItem[size];
		for (int i = 0; i < items.length; i++) {
			items[i] = (ChatItem) list.elementAt(i);
		}
		return items;
	}
	public ChatItem[] sieveChatItem(ChatItem[] v,int s){
		if( v == null ){
			return null;
		}
		int size = v.length;
		if( size < 1){
			return null;
		}
    	if( s == STATE_QUAN){
    		Vector vc = new Vector();
    		for (int i = 0; i < size; i++) {
    			byte state = 1;
    			switch (v[i].m_type) {
    				case ChatItem.TYPE_SI:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_PRIVATE];
    					break;
    				case ChatItem.TYPE_TEAM:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_RANKS];
    					break;
    				case ChatItem.TYPE_WORLD:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_WORLD];
    					break;
    				case ChatItem.TYPE_XI:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_SYSTEM];
    					break;
    				case ChatItem.TYPE_FAMILY:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_FMAILY];
    					break;
    				case ChatItem.TYPE_REALM:
    					state = GameManager.s_system_infos[GameManager.SET_CHANNEL_CAMP];
    					break;
    			}
    			if (state != 0) {
    				vc.addElement(v[i]);
    			}
    		}
    		int n = vc.size();
    		if( n <= 0){
    			vc.removeAllElements();
    			vc = null;
    			return null;
    		}else {
    			ChatItem[] item = new ChatItem[n];
    			for(int i=0;i<n;i++){
    				item[i] = (ChatItem)vc.elementAt(i);
    			}
    			vc.removeAllElements();
    			vc = null;
    			return item;
			}
    	}
		return v;
	}
	private byte getChatType(int state)
	{
		switch (state) {
			case STATE_QUAN:
				return ChatItem.TYPE_QUAN;
			case STATE_WORLD:
				return ChatItem.TYPE_WORLD;
			case STATE_REALM:
				return ChatItem.TYPE_REALM;
			case STATE_FAMILY:
				return ChatItem.TYPE_FAMILY;
			case STATE_TEAM:
				return ChatItem.TYPE_TEAM;
			case STATE_XI:
				return ChatItem.TYPE_XI;
			case STATE_SI:
				return ChatItem.TYPE_SI;
		}
		return ChatItem.TYPE_QUAN;
	}

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE_CHAT, STR_DESCRIBE, true, true);
		// 标签
		paintTop(g);
		// 内容框
		paintContents(g);
		// 下拉条
		paintDrag(g);
		// 描述
	}

	private void paintTop(Graphics g)
	{
		int tempY = m_y + Y_LABEL;
//		paintLabelArr(g, m_x + X_ARR_LEFT, tempY, true);
		paintLabels(g, m_x + X_LABEL_FIRST, tempY);
//		paintLabelArr(g, m_x + X_ARR_RIGHT, tempY, false);
	}

	private void paintContents(Graphics g)
	{
		int tempX = m_x + UITools.INITX_CONTENT;// 位置固定
		int tempY = m_y + Y_RECT;
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int W_RECT = UITools.W_RECT_DRAG;
		UITools.paintContentRect(g, tempX, tempY, W_RECT, H_RECT);
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		g.setClip(tempX, tempY, W_RECT - (UITools.IW_RECT_CONTENT << 1), H_RECT - (UITools.IH_RECT_CONTENT << 1));
		paintItems(g, tempX, tempY, W_RECT - (UITools.IW_RECT_CONTENT << 1), H_CONTENT);
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintDrag(Graphics g)
	{
		UITools.paintScrollBar(g, m_x + UITools.X_DRAG, m_y + Y_RECT, H_RECT, m_plan);
	}

	private void paintLabels(Graphics g, int x, int y)
	{
		int tempX = x;
		int startIndex = STATE_FIRST - STATE_QUAN;
		int w = W_LABEL;
		for (int i = 0; i < NUM_LABEL_DIS; i++) {
			boolean isSel = m_state == STATE_FIRST + i;
			UITools.paintLabelImage(g, tempX, y, w, UITools.H_LABEL, IMG_LABELS[startIndex + i],
					isSel);
			tempX += w + IW_LABEL;
		}
	}

	private void paintItems(Graphics g, int x, int y, int w, int h)
	{
		if (m_items_curr == null) {
			return;
		}
		int tempX = x + UITools.IW_RECT_CONTENT;
		int tempY = y + m_y_top;
		int w_content = w - (UITools.IW_RECT_CONTENT << 1);
		int sumH = 0;// 限制
		for (int i = 0; i < m_items_curr.length; i++) {
			if (m_items_curr[i] == null) {
				continue;
			}
			int H = m_items_curr[i].getH();
			if (m_y_top + sumH + H < 0) {// 在屏幕之上
				tempY += H;
				sumH += H;
			}
			else if (m_y_top + sumH >= H_CONTENT) {// 屏幕以下
				return;
			}
			else {
				boolean isSel = m_index == i;
				if (isSel) {
					UITools.paintInputRect(g, x, tempY, w, H, isSel);
				}
				m_items_curr[i].paintChatItem(g, tempX, tempY + 1, w_content, H, isSel);
				tempY += H;
				sumH += H;
			}
		}
	}

}
